package com.sgw.networkapp.ui.dashboard;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.sgw.networkapp.databinding.FragmentDashboardBinding;
import com.sgw.networkapp.utils.network1.NetWorkMonitorManager;
import com.sgw.networkapp.utils.network1.NetWorkState;

public class DashboardFragment extends Fragment {

    private FragmentDashboardBinding binding;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        DashboardViewModel dashboardViewModel =
                new ViewModelProvider(this).get(DashboardViewModel.class);

        binding = FragmentDashboardBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        final TextView textView = binding.textDashboard;
        dashboardViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);
        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
    @Override
    public void onStart() {
        super.onStart();
        NetWorkMonitorManager.getInstance().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        NetWorkMonitorManager.getInstance().unregister(this);
    }
    //不加注解默认监听所有的状态，方法名随意，只需要参数是一个NetWorkState即可
//@NetWorkMonitor(monitorFilter = {NetWorkState.GPRS})//只接受网络状态变为GPRS类型的消息
    public void onNetWorkStateChange(NetWorkState netWorkState) {
        Log.i("TAG", "onNetWorkStateChange >>> :" + netWorkState.name());
    }
}